<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <!-- 新增角色按钮 -->
        <el-row style="height:60px">
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            @click="addRole"
          >新增角色</el-button>
          <!-- 新增弹框 -->
          <el-dialog :title="roleForm.id?'编辑角色':'新增角色'" :visible="showDialog" @close="closeDialog">
            <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
              <el-form-item label="角色名称" prop="name">
                <el-input v-model="roleForm.name" />
              </el-form-item>
              <el-form-item label="角色描述">
                <el-input v-model="roleForm.description" />
              </el-form-item>
            </el-form>
            <!-- 底部 -->
            <el-row slot="footer" type="flex" justify="center">
              <el-col :span="6">
                <el-button size="small" @click="closeDialog">取消</el-button>
                <el-button size="small" type="primary" @click="btnOK">确定</el-button>
              </el-col>
            </el-row>
          </el-dialog>
        </el-row>
        <!-- 表格 -->
        <el-table :data="list">
          <el-table-column
            label="序号"
            width="120"
            type="index"
          />
          <el-table-column label="角色名称" width="240" prop="name" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="scope">
              <el-button size="small" type="success" @click="assignPermi(scope.row.id)">分配权限</el-button>
              <el-button size="small" type="primary" @click="editRole(scope.row.id)">编辑</el-button>
              <el-button size="small" type="danger" @click="delRole(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <!-- 分页组件 -->
          <el-pagination
            layout="prev,pager,next"
            :page-size="params.pagesize"
            :current-page="curPage"
            :total="total"
            @current-change="currentPage"
          />
        </el-row>
      </el-card>
    </div>
    <!-- 分配权限 弹窗 -->
    <assign-permi :role-id="roleId" :show-assign-dialog="showAssignDialog" @close-dialog="closeAssignDialog" />
  </div>
</template>

<script>
import { getRoleList, deleteRole, addRole, getRoleDetail, updateRole } from '@/api/setting'
import AssignPermi from './components/assign-permi.vue'
export default {
  components: {
    AssignPermi
  },
  data() {
    return {
      curPage: 0, // 当前页
      list: [],
      params: {
        page: 1,
        pagesize: 10
      },
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      },
      showDialog: false, // 弹窗是否开启
      total: 0,
      showAssignDialog: false,
      roleId: null
    }
  },
  computed: {
    // 当前页记录是否为一条
    allPage() {
      return this.total % this.params.pagesize === 0
    },
    // 最大页数
    maxPage() {
      return Math.ceil(this.total / this.params.pagesize)
    }
  },
  mounted() {
    this.hgetRoleList()
  },
  methods: {
    // 获取角色列表
    async hgetRoleList() {
      const res = await getRoleList(this.params)
      this.list = res.rows
      this.total = res.total
    },
    // 页数改变时，改变数组中的值
    currentPage(page) {
      this.params.page = page
      this.hgetRoleList()
    },
    // 删除角色
    delRole(role) {
      this.$confirm('是否删除该角色?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 发送数据
        await deleteRole(role)
        // (删除成功后，)当我们再次发起请求的时候，如果页面还有一条数据，当前页-- ，当前页大于等于2 有资格
        //
        // 优化，短路运算
        this.list.length === 1 && this.params.page > 1 && this.params.page--
        // 刷新列表  渲染
        this.hgetRoleList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 添加角色
    addRole() {
      this.showDialog = true
      // 一共多少页
    },
    // 修改角色
    async editRole(id) {
      this.showDialog = true
      const res = await getRoleDetail(id)
      this.roleForm = res
    },
    // 关闭弹窗
    closeDialog() {
      this.showDialog = false
      // 初始化
      this.roleForm = {
        name: '',
        description: ''
      }
      // 清除校验痕迹
      this.$refs.roleForm.resetFields()
    },
    // 提交
    btnOK() {
      this.$refs.roleForm.validate(async(res) => {
        if (res) {
          if (this.roleForm.id) {
            await updateRole(this.roleForm)
            this.$message.success('修改成功')
          } else {
            await addRole(this.roleForm)
            this.hgetRoleList()

            this.allPage ? this.params.page = this.maxPage + 1 : this.params.page = this.maxPage
            // this.curPage = this.params.page
            // console.log(this.curPage)
            this.$message.success('添加成功')
          }

          // 刷新列表
          this.hgetRoleList()
          this.curPage = this.params.page
          // this.getPage()
          // 关闭弹窗
          this.closeDialog()
        }
      })
    },
    // 分配权限
    assignPermi(id) {
      this.showAssignDialog = true
      this.roleId = id
    },
    closeAssignDialog() {
      this.showAssignDialog = false
    }
  }

}
</script>

<style>

</style>
